﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMeterParamFrame.Model.DIValueConverters
{

    /// <summary>
    /// 阶梯电价，转换类，可以转换 
    /// xxxxxx.xx类型的数据
    /// </summary>
    class Converter_JeiTiZhi : IDIDataConverter
    {
        static uint MIN_JEITI1 = uint.Parse("04060001", System.Globalization.NumberStyles.AllowHexSpecifier);
        static uint MAX_JEITI1 = uint.Parse("040600FF", System.Globalization.NumberStyles.AllowHexSpecifier);

        static uint MIN_JEITI2 = uint.Parse("04060201", System.Globalization.NumberStyles.AllowHexSpecifier);
        static uint MAX_JEITI2 = uint.Parse("040602FF", System.Globalization.NumberStyles.AllowHexSpecifier);

        static uint SHENG_YU_JINGE = uint.Parse("00900200", System.Globalization.NumberStyles.AllowHexSpecifier);

        public bool CanConverter(string di)
        {
            uint udi = uint.Parse(di, System.Globalization.NumberStyles.AllowHexSpecifier);

            if (udi >= MIN_JEITI1 && udi <= MAX_JEITI1)
                return true;

            if (udi >= MIN_JEITI2 && udi <= MAX_JEITI2)
                return true;

            if (udi == SHENG_YU_JINGE)
                return true;

            return false;
        }

        public string ConvertToUI(string di, string lowValue)
        {
            if (lowValue.Length != 8)
            {
                throw new ArgumentException("DI(" + di + "),Value(" + lowValue + ")");
            }

            string[] vals = new string[2];
            string newValue = "";

            vals[0] = lowValue.Substring(0, 6);
            vals[1] = lowValue.Substring(6, 2);

            //处理整数部分
            int lastPositiveIndex = vals[0].IndexOfAny(DIValueConverter.NUMBER_POSITIVE);
            if (lastPositiveIndex >= 0)
            {
                newValue = vals[0].Substring(lastPositiveIndex, vals[0].Length - lastPositiveIndex);
            }
            else
            {
                newValue = "0";
            }

            //小数点
            newValue += ".";

            //小数部分
            lastPositiveIndex = vals[1].LastIndexOfAny(DIValueConverter.NUMBER_POSITIVE);
            if (lastPositiveIndex >= 0)
            {
                newValue += vals[1].Substring(0, lastPositiveIndex + 1);
            }
            else
            {
                newValue += "0";
            }

            return newValue;
        }

        public string ConvertToLow(string di, string uiValue)
        {
            string newValue = "";
            string[] ss = uiValue.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);

            ss[0] = ss[0].PadLeft(6, '0');
            newValue = ss[0];
            if (ss.Length >= 2)
            {
                ss[1] = ss[1].PadRight(2, '0');
                newValue += ss[1];
            }
            else
            {
                newValue += "00";
            }
            if (newValue.Length != 8)
                throw new Exception("数据格式不对");
            return newValue;
        }

        public bool ValidateValue(string di, string value)
        {
            throw new NotImplementedException();
        }
    }
}
